package com.pxmeta.file.service;

import cn.hutool.core.lang.ObjectId;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.pxmeta.common.config.OssProperties;
import com.pxmeta.common.exception.ServiceException;
import com.pxmeta.common.utils.ThreadPoolExecutorUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.time.LocalDate;
import java.util.concurrent.atomic.AtomicReference;

@Service
@RequiredArgsConstructor
public class OssService {
    @Value("${aliyun.accessKeyId}")
    private String accessKeyId;

    @Value("${aliyun.accessKeySecret}")
    private String accessKeySecret;

    public String updateFile(MultipartFile file){

        String endpoint = OssProperties.END_POINT;
        String accessKeyId = OssProperties.ACCESS_KEY_ID;
        String accessKeySecret = OssProperties.ACCESS_KEY_SECRET;
        String bucketName = OssProperties.BUCKET_NAME;
        String url = "";
        //创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        String fileName = file.getOriginalFilename();
        try{
            //获取上传文件输入流
            InputStream inputStream = file.getInputStream();
            String fileId = ObjectId.next();
            String year = LocalDate.now().getYear() + "";
            fileName = year + "/" + fileId + fileName;
            ossClient.putObject(bucketName, fileName, inputStream);

        }catch (Exception e){
            throw new RuntimeException("文件上传失败",e);
        }finally {
            ossClient.shutdown();
        }

        url = "https://" + bucketName + "." + endpoint + "/" + fileName;
        return url;
    }

    public String uploadVideoAly(MultipartFile file) {
        try {
            //accessKeyId, accessKeySecret
            //fileName：上传文件原始名称
            // 01.03.09.mp4
            String fileName = file.getOriginalFilename();
            //title：上传之后显示名称
            String title = fileName.substring(0, fileName.lastIndexOf("."));
            //inputStream：上传文件输入流
            InputStream inputStream = file.getInputStream();
            UploadStreamRequest request = new UploadStreamRequest(accessKeyId,accessKeySecret, title, fileName, inputStream);
            UploadVideoImpl uploader = new UploadVideoImpl();
            UploadStreamResponse response = uploader.uploadStream(request);
            String videoId = null;
            if (response.isSuccess()) {
                videoId = response.getVideoId();
            } else {
                throw new ServiceException("视频上次失败！");
            }
            return videoId;
        }catch(Exception e) {
            return null;
        }
    }


}
